mod config;
mod features;
mod util;
#[cfg(test)]
mod util_test;
use clap::{Arg, ArgAction, Command};
use common::{file_system::deserialize_toml, model::ProjectConfig};
use features::{apply_alias, apply_proxy};
use std::fs;
use std::path::Path;

// 主函数
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let matches = Command::new("Project features")
        .version("0.1.0")
        .author("caozongying<zongying_cao@163.com>")
        .about("caozongying_cao<zongying_cao@163.com> is the author of project features")
        .arg(
            Arg::new("config")
                .short('c')
                .long("config")
                .value_name("CONFIG_FILE_PATH")
                .help("配置文件的路径")
                .action(ArgAction::Set),
        )
        .arg(
            Arg::new("project_dir")
                .short('p')
                .long("project_dir")
                .value_name("PROJECT_DIR")
                .help("项目路径")
                .action(ArgAction::Set),
        )
        .get_matches();

    let config_file_path = matches
        .get_one::<String>("config")
        .expect("无法获取config的文件路径");

    let project_dir = &matches
        .get_one::<String>("project_dir")
        .expect("无法获取project_dir");

    let project_config =
        &deserialize_toml::<ProjectConfig>(&fs::read_to_string(config_file_path)?)?;
    let project_path = Path::new(project_dir);
    apply_proxy(project_config, project_path)?;
    apply_alias(project_config, project_path)?;
    Ok(())
}
